﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Surveying;
namespace GIS_SpatialAnalyst
{
    class Line_Line_RelationClass
    {
        public Point p1;
        public Point p2;
        public Point p3;
        public Point p4;
        public bool CrossVector()
        {
            double d1 = direction(p1, p2, p4);//
            double d2 = direction(p1, p2, p3);//
            double d3 = direction(p3, p4, p1);//
            double d4 = direction(p3, p4, p2);
            if (d1 * d2 < 0 && d3 * d4 < 0)
                return true;
            else if (d1 == 0 && Onsegment(p1, p2, p4))
                return true;
            else if (d2 == 0 && Onsegment(p1, p2, p3))
                return true;
            else if (d3 == 0 && Onsegment(p3, p4, p1))
                return true;
            else if (d4 == 0 && Onsegment(p3, p4, p2))
                return true;
            else
                return false;
        }

        private bool Onsegment(Point p1, Point p2, Point p3)
        {
            double x_min, x_max, y_min, y_max;
            if (p1.x < p2.x)
            {
                x_min = p1.x;
                x_max = p2.x;
            }
            else
            {
                x_min = p2.x;
                x_max = p1.x;
            }
            if (p1.y < p2.y)
            {
                x_min = p1.x;
                x_max = p2.x;
            }
            else
            {
                x_min = p2.y;
                x_max = p1.y;
            }
            if (p3.x < x_min || p3.x > x_max || p3.x < x_min || p3.x > x_max)
                return false;
            else
                return true;
        }

        private double direction(Point p1,Point p2,Point p3)
        {
            Point d1 = new Point(p3.x - p1.x, p3.y - p1.y);
            Point d2 = new Point(p2.x - p1.x, p2.y - p1.y);
            return d1.x * d2.y - d1.y * d2.x;
        }

        public static bool Line_Line_Relation(Line pl1, Line pl2)
        {
            Point a = pl1.points[0];
            Point b = pl1.points[pl1.m_pointCounts - 1];
            Point c = pl2.points[0];
            Point d = pl2.points[pl2.m_pointCounts - 1];
            if ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y) == 0)
            {
                return false;//平行
            }
            double x, y;
            x = ((b.x - a.x) * (c.x - d.x) * (c.y - a.y) - c.x * (b.x - a.x) * (c.y - d.y) + a.x * (b.y - a.y) * (c.x - d.x)) / ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y));
            y = ((b.y - a.y) * (c.y - d.y) * (c.x - a.x) - c.y * (b.y - a.y) * (c.x - d.x) + a.y * (b.x - a.x) * (c.y - d.y)) / ((b.x - a.x) * (c.y - d.y) - (b.y - a.y) * (c.x - d.x));
            if ((x - a.x) * (x - b.x) <= 0 && (x - c.x) * (x - d.x) <= 0 && (y - a.y) * (y - b.y) <= 0 && (y - c.y) * (y - d.y) <= 0)
            {
                return true; //'相交
            }
            else
            {
                return false; //'相交但不在线段上
            }
        }
        public static Point GetIntersection(Line pl1, Line pl2)
        {
            Point a = pl1.points[0];
            Point b = pl1.points[pl1.m_pointCounts - 1];
            Point c = pl2.points[0];
            Point d = pl2.points[pl2.m_pointCounts - 1];
            double x = ((b.x - a.x) * (c.x - d.x) * (c.y - a.y) - c.x * (b.x - a.x) * (c.y - d.y) +
                              a.x * (b.y - a.y) * (c.x - d.x)) / ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y));
            double y = ((b.y - a.y) * (c.y - d.y) * (c.x - a.x) - c.y * (b.y - a.y) * (c.x - d.x) +
                              a.y * (b.x - a.x) * (c.y - d.y)) / ((b.x - a.x) * (c.y - d.y) - (b.y - a.y) * (c.x - d.x));
            return new Point(x, y);
        }
    }
   
}
